// pages/suggest/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[{
      id:0,
      value:"体验问题",
      isActive:true


    },{
        id: 1,
        value: "商品投诉",
        isActive: false

    }

    ],
    tempFilePaths:[],
    textVal:"",
    imgSrc:[]

  },
  
  tabsItemChange(e){
    const index=e.detail.index;
    let tabs=this.data.tabs;
    tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
    this.setData({
      tabs:tabs
    });
 
 
 
   },
   handleChooseImg(){
     wx.chooseImage({
       count:5,
       sizeType:["original","compressed"],
       sourceType:["album","camera"],

       success: (res) => {
         console.log(res)
        
         this.setData({
           tempFilePaths:[...this.data.tempFilePaths,...res.tempFilePaths]
         })

       },
     })

   },
   handleDelete(e){
     const index=e.currentTarget.dataset.index;
     let {tempFilePaths}=this.data;
     tempFilePaths.splice(index,1);
     this.setData({
       tempFilePaths:tempFilePaths
     })
     

   },
   handleTextInput(e){
     this.setData({
       textVal:e.detail.value
     })

   },
   formSubmit(){
     const {textVal,tempFilePaths}=this.data;
     if(!textVal.trim())
     {
       wx.showToast({
         title: '输入不合法',
         mask:true,
         icon:'none'
       })
       return;
     }
     wx.showLoading({
       title: '正在加载中',
       mask:true
     })
     if(tempFilePaths.length!=0){
     tempFilePaths.forEach((v,i)=>{
        wx.cloud.uploadFile({
          cloudPath:"img"+new Date().getTime()+".jpg",
          filePath:v,
          success:(res)=>{
            console.log(res);
            console.log(res.fileID);
            let url=[res.fileID];
            this.setData({
              imgSrc:[...this.data.imgSrc,...url]
            })

            if(i===tempFilePaths.length-1){
              wx.hideLoading();
              console.log("发送完毕");
              this.setData({
                imgSrc:[],
                textVal:"",
                tempFilePaths:[]
              })
              wx.navigateBack({
                delta:1,
              })
            }
          },
          fail:(error)=>{
            console.log(error)
          }
        })
     })
    }
    else{
      wx.hideLoading({
        complete: (res) => {
          console.log("只提交文本")
        },
      }),
      wx.navigateBack({
       delta:1
      })
      
    }
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})